﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WaveMaster.Holidays.Club.Gateway;
using System.Collections;
using MySql.Data.MySqlClient;
using WaveMaster.Holidays.Club.SQL;
using WaveMaster.Holidays.Club.Domain;

namespace WaveMaster.Holidays.Club.Service
{
    public class MemberTypeService : IService
    {
        DataBaseConnectivity m_dataBaseConectivity;
        public MemberTypeService(DataBaseConnectivity dataBaseConectivity)
        {
            m_dataBaseConectivity = dataBaseConectivity;
        }
        public MemberTypeService()
        {
            m_dataBaseConectivity = new DataBaseConnectivity();
        }
        public void InsertMemberType(MemberType memberType)
        {
            try
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Open();
                MySqlCommand cmd = new MySqlCommand(MemberTypeSQL.InsertMemberType(), m_dataBaseConectivity.InterfaceSqlConnection);
                cmd.Parameters.Add("m_member_type_code", MySqlDbType.VarChar).Value = memberType.m_member_type_code;
                cmd.Parameters.Add("m_member_type_name", MySqlDbType.VarChar).Value = memberType.m_member_type_name;
                cmd.Parameters.Add("m_price", MySqlDbType.Decimal).Value = memberType.m_price;
                cmd.Parameters.Add("m_discount", MySqlDbType.Decimal).Value = memberType.m_discount;
                cmd.Parameters.Add("m_remarks", MySqlDbType.VarChar).Value = memberType.m_remarks;
                cmd.Parameters.Add("m_cretnDate", MySqlDbType.Datetime).Value = DateTime.Now;
                cmd.Parameters.Add("m_cretnBy", MySqlDbType.Int32).Value = memberType.m_cretnBy;
                cmd.Parameters.Add("m_modfctnDate", MySqlDbType.Datetime).Value = DateTime.Now;
                cmd.Parameters.Add("m_modfctnBy", MySqlDbType.Int32).Value = memberType.m_cretnBy;
                cmd.ExecuteNonQuery();
            }
            catch (Exception x)
            {
                LogService.WriteLogException(x);
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Close();
            }
        }
        public void UpdateMemberType(MemberType memberType)
        {
            try
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Open();
                MySqlCommand cmd = new MySqlCommand(MemberTypeSQL.UpdateMemberType(), m_dataBaseConectivity.InterfaceSqlConnection);
                cmd.Parameters.Add("m_member_type_code", MySqlDbType.VarChar).Value = memberType.m_member_type_code;
                cmd.Parameters.Add("m_member_type_name", MySqlDbType.VarChar).Value = memberType.m_member_type_name;
                cmd.Parameters.Add("m_price", MySqlDbType.Decimal).Value = memberType.m_price;
                cmd.Parameters.Add("m_discount", MySqlDbType.Decimal).Value = memberType.m_discount;
                cmd.Parameters.Add("m_remarks", MySqlDbType.VarChar).Value = memberType.m_remarks;
                cmd.Parameters.Add("m_modfctnDate", MySqlDbType.Datetime).Value = DateTime.Now;
                cmd.Parameters.Add("m_modfctnBy", MySqlDbType.Int32).Value = memberType.m_modfctnBy;
                cmd.Parameters.Add("m_member_type_id", MySqlDbType.Int32).Value = memberType.m_member_type_id;
                cmd.ExecuteNonQuery();
            }
            catch (Exception x)
            {
                LogService.WriteLogException(x);
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Close();
            }
        }
        public IList LoadAllMemberType()
        {
            IList result = new ArrayList();
            try
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Open();
                MySqlCommand cmd = new MySqlCommand(MemberTypeSQL.GetAllMemberType(), m_dataBaseConectivity.InterfaceSqlConnection);
                MySqlDataReader rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    MemberType user = new MemberType(rd);
                    result.Add(user);
                }
            }
            catch (Exception x)
            {
                LogService.WriteLogException(x);
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Close();
            }
            return result;
        }
        public MemberType GetMemberTypeByID(long id)
        {
            MemberType result = null;
            try
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Open();
                MySqlCommand cmd = new MySqlCommand(MemberTypeSQL.GetMemberTypeByID(), m_dataBaseConectivity.InterfaceSqlConnection);
                cmd.Parameters.Add("m_member_type_id", MySqlDbType.Int32).Value = id;
                MySqlDataReader rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    result = new MemberType(rd);
                }
            }
            catch (Exception x)
            {
                LogService.WriteLogException(x);
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Close();
            }
            return result;
        }
        public void DeleteMemberType(MemberType user)
        {
            try
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Open();
                MySqlCommand cmd = new MySqlCommand(MemberTypeSQL.DeleteMemberTypeByID(), m_dataBaseConectivity.InterfaceSqlConnection);
                cmd.Parameters.Add("m_member_type_id", MySqlDbType.Int32).Value = user.m_member_type_id;
                cmd.ExecuteNonQuery();
            }
            catch (Exception x)
            {
                LogService.WriteLogException(x);
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Close();
            }
        }
        public IList SearchMemberTypeByCodeName(string p)
        {
            IList result = new ArrayList();
            try
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Open();
                MySqlCommand cmd = new MySqlCommand(MemberTypeSQL.SearchMemberTypeByCodeName(p), m_dataBaseConectivity.InterfaceSqlConnection);
                MySqlDataReader rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    result.Add(new MemberType(rd));
                }
            }
            catch (Exception x)
            {
                LogService.WriteLogException(x);
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Close();
            }
            return result;
        }
    }
}
